package C039;

/**
 * ClassName: C02
 * Package: C039
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/1/31 14:02
 * @Version 1.0
 * @Since 1.0
 */
public class C02 {
    public static void main(String[] args) {
        System.out.println(decodeString("abc3[cd]xyz"));
    }

    static int where = 0;
    public static String decodeString(String s) {
        where = 0;
        return process(s.toCharArray());
    }

    private static String process(char[] charArray) {
        int cur = 0;
        StringBuilder res = new StringBuilder("");
        while(where < charArray.length && charArray[where] != ']') {
            if(charArray[where] >= 'a' && charArray[where] <= 'z') {
                res.append(charArray[where]);
                where ++;
            } else if (charArray[where] >= '0' && charArray[where] <= '9') {
                cur = cur * 10 + charArray[where] - '0';
                where ++;
            } else { // 遇到了[ 这个符号
                where ++;
                appends(process(charArray), cur, res);
                cur = 0;
            }
        }
        where ++;
        return res.toString();
    }

    private static void appends(String process, int cur, StringBuilder res) {
        for (int i = 0; i < cur; i++) {
            res.append(process);
        }
    }
}
